#!/bin/bash

if [ $# -lt 1 ] ; then
    echo "使用方法：$0 pid"
    echo "   pid - 使用Top命令获取的用户进程的ID"
    echo "  "
    echo ""
    exit 0
fi

printf "=================================进程信息===========================================\n" > /tmp/stack.txt
printf "PID[hex]\tCPU\n" >> /tmp/stack.txt

counter=-1
for line in `top -H -p $1 -n 1` 
do
	if [ $line = "COMMAND" ] ; then
       counter=1
    elif [ $counter -gt -1 ] ; then
    	if [[ ! -z `echo $line | grep "[[:cntrl:]]"` ]] ; then 
    		counter=1
    	elif [ $counter -eq 2 ] ; then
            newline=`echo "obase=16;$line"|bc`
    		printf "$line[0x${newline,,}]\t" >> /tmp/stack.txt
    	elif [ $counter -eq 10 ] ; then
    		printf "$line\n" >> /tmp/stack.txt
    	fi

        (( counter ++))  
    fi
done
printf "\n\n" >> /tmp/stack.txt
printf "=================================堆栈信息===========================================\n" >> /tmp/stack.txt
jstack -l $1 >> /tmp/stack.txt